package com.flame.workflow.adapter;

import java.io.File;

import com.flame.file.entity.FileInfo;
import com.flame.file.service.FileInfoService;
import com.flame.workflow.exception.WorkflowExcetionMessage;
import com.flame.workflow.exception.WorkflowRuntimeException;
import com.flame.workflow.ru.service.FileStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

/**
 *
 *
 * @author wuxintong😺😺😺
 * @since 2023/11/7
 */
@Service
public class FileStorageServiceImpl implements FileStorageService {

    @Autowired
    private FileInfoService fileInfoService;

    @Override
    public String saveFile(MultipartFile file) {
        return String.valueOf(fileInfoService.save(file));
    }

    @Override
    public File getFile(String fileId) {
        FileInfo fileInfo = fileInfoService.findById(Long.valueOf(fileId));
        if (ObjectUtils.isEmpty(fileInfo)) {
            throw new WorkflowRuntimeException(WorkflowExcetionMessage.FILE_NOT_EXIST);
        }
        return new File(fileInfo.getFileLocation());
    }
}
